package com.achievement.service.impl;


import com.achievement.domain.dto.AllAchievementDTO;
import com.achievement.domain.entity.AllAchievementEntity;
import com.achievement.domain.param.AllAchievementParam;
import com.achievement.domain.vo.AllAchievementVO;
import com.achievement.domain.vo.ProjectProductVO;
import com.achievement.mapper.AllAchievementMapper;
import com.achievement.mappings.AllAchievementMapping;
import com.achievement.service.IAllAchievementService;
import com.achievement.service.IProjectProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.DateUtils;
import com.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AllAchievementServiceImpl extends ServiceImpl<AllAchievementMapper, AllAchievementEntity> implements IAllAchievementService {
    @Autowired
    private IProjectProductService projectProductService;
    @Autowired
    private AllAchievementMapping mapping;

    @Override
    public List<AllAchievementVO> selectByPage(AllAchievementParam param) {
        param.setProductDateEnd(DateUtils.dateStrHandle(param.getProductDateEnd(), param.getProductDateFormat(), "end"));
        param.setProductDateStart(DateUtils.dateStrHandle(param.getProductDateStart(), param.getProductDateFormat(), "start"));
        List<AllAchievementVO> voList = this.baseMapper.getList(param,SecurityUtils.getUsername(),SecurityUtils.getUserId().toString());
        return voList;
    }

    @Override
    public Map<String, Object> group() {
        List<Map<String,String>> byType = this.baseMapper.groupByType(SecurityUtils.getUserId().toString());
        List<Map<String, String>> byYear = this.baseMapper.groupByYear(SecurityUtils.getUserId().toString());
        //主持的成果
        Integer zcCount = this.baseMapper.countByFirstAuthor(SecurityUtils.getUsername(),SecurityUtils.getUserId().toString());
        //个人总的成果
        Integer count = this.baseMapper.countByPIds(SecurityUtils.getUserId().toString());
        //参与成果
        Integer cyCount=count-zcCount;
        Map<String,Object> joinTypeMap=new HashMap<>();
        joinTypeMap.put("主持",zcCount);
        joinTypeMap.put("参与",cyCount);
        Map<String,Object> map=new HashMap<>();
        map.put("type",byType);
        map.put("year",byYear);
        map.put("joinType",joinTypeMap);
        return map;
    }

    @Override
    public List<AllAchievementVO> selectByProjectId(String projectId) {
        List<ProjectProductVO> projectProductVOS = projectProductService.selectByProjectId(projectId);
        if(projectProductVOS == null || projectProductVOS != null && projectProductVOS.size() <= 0){
            return new ArrayList<>();
        }else{
            List<AllAchievementDTO> dtos = new ArrayList<>();
            projectProductVOS.stream().forEach(a -> {
                AllAchievementDTO dto = new AllAchievementDTO();
                dto.setId(a.getProductId());
                dto.setProductType(a.getProductType());
                dtos.add(dto);
            });
//        List<AllAchievementVO> voList = this.baseMapper.selectByDTO(dtos);
            List<AllAchievementVO> voList = this.baseMapper.selectByDTONew(dtos);
            return voList;
        }
    }
}
